package com.edu.controller;

import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.edu.model.Board;
import com.edu.model.Piece;
import com.edu.model.PieceType;
import com.edu.model.Position;
import com.edu.util.ChessHelper;
import com.edu.view.ChessView;

public class ChessController {

	private Board board;

	private ChessView chessView;

	
	// 初始化工作
	public ChessController() {
		Map<String, Piece> pieces = initPieces();
		initBoard(pieces);
		chessView = new ChessView();

		EventQueue.invokeLater(() -> {
			//board = new Board("welcome", 500, 500, stones);
			//board.addMouseListener(new ChessMouseListener());
		});
	}

	
	private void initBoard(Map<String, Piece> pieces) {
		board = new Board(pieces);
	}


	private Map<String, Piece> initPieces() {
		Map<String, Piece> pieces = new HashMap<>();
		pieces.put(PieceType.B_JU_1.getName(), new Piece(new Position(0, 0), PieceType.B_JU_1));
		pieces.put(PieceType.B_MA_1.getName(), new Piece(new Position(1, 0), PieceType.B_MA_1));
		pieces.put(PieceType.B_XIANG_1.getName(), new Piece(new Position(2, 0), PieceType.B_XIANG_1));
		pieces.put(PieceType.B_SHI_1.getName(), new Piece(new Position(3, 0), PieceType.B_SHI_1));
		pieces.put(PieceType.B_JIANG.getName(), new Piece(new Position(4, 0), PieceType.B_JIANG));
		pieces.put(PieceType.B_SHI_2.getName(), new Piece(new Position(5, 0), PieceType.B_SHI_2));
		pieces.put(PieceType.B_XIANG_2.getName(), new Piece(new Position(6, 0), PieceType.B_XIANG_2));
		pieces.put(PieceType.B_MA_2.getName(), new Piece(new Position(7, 0), PieceType.B_MA_2));
		pieces.put(PieceType.B_JU_2.getName(), new Piece(new Position(8, 0), PieceType.B_JU_2));
		pieces.put(PieceType.B_PAO_1.getName(), new Piece(new Position(1, 2), PieceType.B_PAO_1));
		pieces.put(PieceType.B_PAO_2.getName(), new Piece(new Position(7, 2), PieceType.B_PAO_2));
		pieces.put(PieceType.B_ZU_1.getName(), new Piece(new Position(0, 3), PieceType.B_ZU_1));
		pieces.put(PieceType.B_ZU_2.getName(), new Piece(new Position(2, 3), PieceType.B_ZU_2));
		pieces.put(PieceType.B_ZU_3.getName(), new Piece(new Position(4, 3), PieceType.B_ZU_3));
		pieces.put(PieceType.B_ZU_4.getName(), new Piece(new Position(6, 3), PieceType.B_ZU_4));
		pieces.put(PieceType.B_ZU_5.getName(), new Piece(new Position(8, 3), PieceType.B_ZU_5));

		pieces.put(PieceType.R_JU_1.getName(), new Piece(new Position(0, 9), PieceType.R_JU_1));
		pieces.put(PieceType.R_MA_1.getName(), new Piece(new Position(1, 9), PieceType.R_MA_1));
		pieces.put(PieceType.R_XIANG_1.getName(), new Piece(new Position(2, 9), PieceType.R_XIANG_1));
		pieces.put(PieceType.R_SHI_1.getName(), new Piece(new Position(3, 9), PieceType.R_SHI_1));
		pieces.put(PieceType.R_SHUAI.getName(), new Piece(new Position(4, 9), PieceType.R_SHUAI));
		pieces.put(PieceType.R_SHI_2.getName(), new Piece(new Position(5, 9), PieceType.R_SHI_2));
		pieces.put(PieceType.R_XIANG_2.getName(), new Piece(new Position(6, 9), PieceType.R_XIANG_2));
		pieces.put(PieceType.R_MA_2.getName(), new Piece(new Position(7, 9), PieceType.R_MA_2));
		pieces.put(PieceType.R_JU_2.getName(), new Piece(new Position(8, 9), PieceType.R_JU_2));
		pieces.put(PieceType.R_PAO_1.getName(), new Piece(new Position(1, 7), PieceType.R_PAO_1));
		pieces.put(PieceType.R_PAO_2.getName(), new Piece(new Position(7, 7), PieceType.R_PAO_2));
		pieces.put(PieceType.R_BING_1.getName(), new Piece(new Position(0, 6), PieceType.R_BING_1));
		pieces.put(PieceType.R_BING_2.getName(), new Piece(new Position(2, 6), PieceType.R_BING_2));
		pieces.put(PieceType.R_BING_3.getName(), new Piece(new Position(4, 6), PieceType.R_BING_3));
		pieces.put(PieceType.R_BING_4.getName(), new Piece(new Position(6, 6), PieceType.R_BING_4));
		pieces.put(PieceType.R_BING_5.getName(), new Piece(new Position(8, 6), PieceType.R_BING_5));
		return pieces;
	}

	private class ChessMouseListener extends MouseAdapter {

		@Override
		public void mouseReleased(MouseEvent event) {
			// 位移
			event.translatePoint(0, - chessView.getInsets().top);
			//获取鼠标点击位置
			Point pt = event.getPoint();
			// 将pt转化为象棋的行列值，
			int row, col;
			Position pos = ChessHelper.getRowCol(pt, chessView.SIDE_LENGTH);
			if (pos == null) { return; } // 点到棋盘之外
			String key  = chessView.getSelectedPieceKey();
			if (StringUtils.isEmpty(key)) {
				//如果没有棋子被选中
				// 判断这个行列值是否有棋子
				boolean empty = board.isEmpty(pos); 
				if (!empty) {
					//如果有子将其选中
					Piece piece = board.getPiece(pos);
					chessView.setSelectedPieceKey(piece.getKey());
					
				} else {
					//如果没子 直接返回
					return;
				}
			} else {
				// 如果有棋子被选中，说明用户需要走子。
				//判断鼠标点击位置是否有子
				boolean empty = board.isEmpty(pos); 
				if (!empty) {
					//如果有子 判断是否可以吃子
					//Piece piece = board.getPiece(pos);
					board.updatePiece(key, pos);
				} else {
					//如果没子 判断是否可以走子
					Piece piece = board.getPiece(pos);
					board.update(piece);
				}
				
			}
			// 更新视图
			chessView.render(piece);

		}
	}
}
